let ErrorUtils = require('../utils/error/ErrorUtils');
let {Storage} = require('../models');
const {infoLogger, errorLogger} = require('../config').logger;
const Joi = require('joi');
const perpage = 10;
const lodash = require('lodash');

class StoreService {
    /**
     * 城市添加
     * @returns {Promise<*>}
     */
    static async index(id) {
        infoLogger.info(">>>>>获取城市添加开始");
        return await Storage.findById(id);
    }

    /**
     * 获取城市列表
     * @returns {Promise<void>}
     */
    static async list(page) {
        infoLogger.info(">>>>>获取店铺列表开始");
        if (!page) {
            page = 1;
        }
        page = parseInt(page);
        if (isNaN(page)) {
            ErrorUtils.throwApiError(ErrorUtils.REQUEST_ERROR);
        }
        let skip = (page - 1) * perpage;
        let result = await Storage
            .find()
            .populate({
                select: "name",
                path: "cityId",
            })
            .skip(skip)
            .limit(perpage)
            .lean()
            .exec();
        return result;
    }

    /**
     * 获取城市列表
     * @returns {Promise<void>}
     */
    static async list1(cityId) {
        infoLogger.info(">>>>>获取店铺列表开始");
        let result = await Storage
            .find()
            .where("cityId", cityId)
            .limit(40)
            .lean()
            .exec();
        return result;
    }

    /**
     * 获取门店详情
     * @returns {Promise<*>}
     */
    static async detail(id) {
        if (!id) {
            ErrorUtils.throwApiError(ErrorUtils.ID_INVALID);
        }
        infoLogger.info(">>>>>获取门店详情");
        return await Storage.findById(id);
    }

    static checkStorageData(data) {
        let schema = Joi.object({
            name: Joi.string().min(2).max(20).required(),
            phone: Joi.string().min(11).max(11).required(),
            des: Joi.string().min(1).max(100).required(),
            logo: Joi.string().min(10).max(100).required(),
            startTime: Joi.string().required(),
            endTime: Joi.string().required(),
            address: Joi.string().required(),
            cityId: Joi.string().required(),
            lng: Joi.number(),
            lat: Joi.number(),
        });
        let result = Joi.validate(data, schema);
        return result;
    }

    /**
     * 城市添加
     * @returns {Promise<*>}
     */
    static async create(data) {
        infoLogger.info(">>>>>:获取城市添加开始");
        await StoreService.checkStorageData(data);
        let result = await Storage.findOne({name: data.name});
        if (!result) {
            return await Storage.create(data);
        }
        return result;
    }

    /**
     * 城市添加
     * @returns {Promise<*>}
     */
    static async update(data) {
        infoLogger.info(">>>>>获取城市添加开始");
        let id = data._id;
        if (!id) {
            ErrorUtils.throwApiError(ErrorUtils.ID_INVALID);
        }
        data = lodash.omit(data, ['id', '_id']);
        await StoreService.checkStorageData(data);
        let res = await Storage.findById(id);
        if (!res) {
            ErrorUtils.throwApiError(ErrorUtils.DATA_NOT_EXIST);
        }
        let result = await Storage.update({_id: id}, data);
        return result;

    }

    /**
     * 城市删除
     * @returns {Promise<*>}
     */
    static async delete(id) {
        infoLogger.info(">>>>>获取城市添加开始");
        if (!id) {
            return false;
        }
        await Storage.remove({_id: id});
        return true;
    }
}

module.exports = StoreService;
